
ticketHub.controller('internationCtrls',function($scope,$http,$state){	
	$scope.routeMess_right = true;//是否含税的div的隐藏，数据加载后显示
	$scope.OR = true;             //默认为单程
	$scope.chooseOneWay = function(){
		$scope.OR = true;
	}
	$scope.chooseRoundWay = function(){
		$scope.OR = false;
	}
	//请求参数；
	$scope.fromCity = "";
	$scope.toCity = "";
	$scope.fromDate = "";
	$scope.toDate = "";
	$scope.airlineCompany = "";
	$scope.cabinRank = "";
	$scope.supplierId = "";
	$scope.adultNum = "1";
	$scope.childNum = "0";
	//$watch儿童数量不能大于成人数量
	$scope.$watch('childNum',function(newVal,oldVal){
		if($scope.adultNum<$scope.childNum){
			$scope.childNum = oldVal;
			layer.msg("儿童数量不能大于成人数量",{icon:2});
			return false;
		}
	})		
	
		
	
	
	//点击查询
	$scope.query = function(){			
		var fromCity = $scope.fromCity;
		var toCity = $scope.toCity;
		var fromDate = $scope.fromDate;
		var toDate = $scope.toDate;
		var airlineCompany = $scope.airlineCompany;
		var cabinRank = $scope.cabinRank;
		supplierId = $scope.supplierId;		
		if($scope.OR){
			console.log(fromCity,toCity,fromDate,toDate,supplierId)
			if(fromCity==""||toCity==""||fromDate==""){
				layer.msg("请输入完整的搜索条件");
				return false;
			}
			layer.load(3,{shadeClose:true});
			var postData = {
					"flightRangeType": "OW",
					"cabinRank":cabinRank,
					"supplierId":$scope.supplierId,
					"adultNum":$scope.adultNum,
					"childNum":$scope.childNum,
					"flightRange" : 
						[{											
					    	"fromCity" : fromCity.toUpperCase(),
					    	"toCity" : toCity.toUpperCase(),
					    	"airlineCompany":airlineCompany,
					    	"fromDate" : fromDate					    	
					    }]		
			}
			getAjax(postData);
			
		}else{
			if(fromCity==""||toCity==""||fromDate==""||toDate==""){
				layer.msg("请输入完整的搜索条件");
				return false;
			}
			layer.load(3,{shadeClose:true});
			var postData = {
					"flightRangeType": "RT",
					"cabinRank":cabinRank,
					"supplierId":$scope.supplierId,
					"adultNum":$scope.adultNum,
					"childNum":$scope.childNum,
					"flightRange" : 
						[{											
					    	"fromCity" : fromCity.toUpperCase(),
					    	"toCity" : toCity.toUpperCase(),
					    	"airlineCompany":airlineCompany,
					    	"fromDate" : fromDate
					    },{											
					    	"fromCity" : toCity.toUpperCase(),
					    	"toCity" : fromCity.toUpperCase(),
					    	"airlineCompany":airlineCompany,
					    	"fromDate" : toDate
					    }]		
			}
			getAjax(postData);
			
		}				
	}
	function getAjax(postData){
		$http({
			url:'../flight/search/inter?temp='+Math.random(),
			method:'post',
			data:postData
		})
		.then(function(data){			
			layer.closeAll();	
			var status = data.data.status;			
			if(status == 1001){	
				$scope.flightSegmentResult = data.data.data;
				console.log($scope.flightSegmentResult)
				$scope.routeMess_right = false;
			}
			else{
				layer.alert("错误返回 "+data.data.message);		
			}			
		})
	}
	//默认显示
	$scope.tax = false;  //是否含税按钮的样式
	$scope.qi = true;   //价格的显示
	//不含税价格
	$scope.noTaxPrice = function(){
		$scope.tax = true;
		$scope.qi = false;
	};
	//含税价格
	$scope.taxPrice = function(){
		$scope.tax = false;
		$scope.qi = true;
	};
	//点击航班详情
	$(".main").delegate(".flightDetail","click",function(){
		var target = $(this).parent().parent().parent().parent().find(".detail_2").is(":hidden");
		if(target){
			$(this).parent().parent().parent().parent().find(".detail_2").show(500);
			$(this).find(".iconfont").html("&#xe65a;");
		}else{
			$(this).parent().parent().parent().parent().find(".detail_2").hide(500);
			$(this).find(".iconfont").html("&#xe808;");
		}
	})
	
	//预定
	$scope.toPre = function(flight,index){	
		console.log(flight,index)
		var flight = JSON.stringify(flight);
		$scope.data_ = flight;
	}
	//国际税费价格验证；
	$scope.checkFax = function(flight,index){
		
		var data = {};
		if($scope.OR){
			data.flightRangeType = "OW";
		}else{
			data.flightRangeType = "RT";
		}		
		data.supplierId = supplierId;
		data.cabinRank = flight.prices[index].cabinList[0].cabinRank;
		data.freightLimitQueryID = flight.prices[index].freightLimitQueryID;
		data.passengerNature = "UNLIMITED";
		data.airlineCompany = flight.airlineCompany;//待定
		if(flight.prices[index].policy){
			data.policyID = flight.prices[index].policy[0].policyId;
			data.adultFacePrice = flight.prices[index].policy[0].facePrice;
		}else{
			data.policyID = "";
			data.adultFacePrice = "";
		}		
		
		data.flightRange = [];
		var segments = flight.segments;
		var cabinList = flight.prices[index].cabinList;					
		var setFlightRange = function(){
			var rangeSegmentCountArr = flight.rangeSegmentCount.split(",");
			var length = rangeSegmentCountArr.length;
			for(var i=0;i<length;i++){
				var json = {};
				json.flightScheduled = [];
				data.flightRange.push(json);
			}
		}();
		
		var length = segments.length;
		for(var i=0;i<length;i++){
			if(segments[i].correspondRange=="1"){										
				var flightScheduled = {};				
				flightScheduled.fromAirport = segments[i].fromAirport;
				flightScheduled.toAirport = segments[i].toAirport;
				flightScheduled.flightNo = segments[i].flightNo;
				flightScheduled.cabin = cabinList[i].cabinCode;
				flightScheduled.departTime = segments[i].fromDate;
				flightScheduled.arriveTime = segments[i].toDate;
				flightScheduled.cabinRank = cabinList[i].cabinRank;
				data.flightRange[0].flightScheduled.push(flightScheduled);						
			}
			if(segments[i].correspondRange=="2"){
				var flightScheduled = {};				
				flightScheduled.fromAirport = segments[i].fromAirport;
				flightScheduled.toAirport = segments[i].toAirport;
				flightScheduled.flightNo = segments[i].flightNo;
				flightScheduled.cabin = cabinList[i].cabinCode;
				flightScheduled.departTime = segments[i].fromDate;
				flightScheduled.arriveTime = segments[i].toDate;
				flightScheduled.cabinRank = cabinList[i].cabinRank;
				data.flightRange[1].flightScheduled.push(flightScheduled);
			}			
		}		
		checkFaxData = data;//定义全局变量，在子页面中获取。
		layer.open({
			type: 2,
			title: ['国际税费价格验证','background:#f7f7f7;font-weight:600;color:#333;border-bottom:1px solid #e0e0e0;font-size:14px;'],
			shadeClose: true,
			shade: [0.5,'black'],
			area: ['420px', '220px'],  
			content: ["../html/checkFax.html?","no"],// iframe的url，no代表不显示滚动条
			end:function(){
				
			}
				
		});
	}
})
	
	
